<template>
	<div id="bg" class="w-h-100" style="box-sizing: border-box; overflow: hidden">
		<div class="w-h-100 ver-flex">
			<el-card class="box-card" shadow="never" :body-style="{ paddingBottom: '0' }">
				<search ref="searchRef" v-bind="planSearch">
					<template #footer="scope">
						<el-button type="primary" icon="ele-Search" @click="query"> {{ $t('qEh3zD5bC8bZb4bLvFe29') }} </el-button>
						<el-button icon="ele-Plus" type="primary" @click="add" v-if="$hasPer('trainingPlan:add')"> {{ $t('wnpevud10fwvwhDyynC4A') }} </el-button>
					</template>
				</search>
			</el-card>

			<el-card class="w-h-100 flex-1 box-card" style="margin-top: 16px" shadow="never" :body-style="{ paddingBottom: '0' }">
				<inscroll-table :data-list="list" v-bind="planCourseTableConfig" :dataCount="Number(totalRows)" :handle-size-change="handleSizeChange" :handle-current-change="handleCurrentChange">
					<template #file="scope">
						<div style="cursor: pointer" @click="fileClick(scope.row.v_PlanFilePath)">{{ scope.row.v_PlanFilePath?.length > 0 ? $t('2KyEk7Ir2Lz3bN9ByjSaE') : $t('66gSt7xsIbieLa59Td1H') }}</div>
					</template>
					<template #operation="scope">
						<div>
							<el-button
								icon="ele-Document"
								@click="
									() => {
										detailAction(scope.row);
									}
								"
								type="primary"
								size="small"
								text
							>
								{{ $t('ucC9qxHvkvdh9OGEo1pn') }}
							</el-button>
							<el-button
								icon="ele-Edit"
								@click="
									() => {
										changeAction(scope.row);
									}
								"
								type="primary"
								size="small"
								text
								v-if="$hasPer('trainingPlan:change')"
							>
								{{ $t('kiUkiqa3h0HLymgh7eEb') }}
							</el-button>
							<!-- <el-button icon="ele-Folder" v-if="scope.row.v_PlanFilePath?.length > 0" type="success" @click="pushFile(scope.row.v_PlanFilePath)" text size="small">{{
								$t('s4KbtSrTfOzuWyRcHgTxx')
							}}</el-button> -->

							<el-button
								icon="ele-Delete"
								@click="
									() => {
										deleteAction(scope.row);
									}
								"
								type="danger"
								size="small"
								text
								v-if="$hasPer('trainingPlan:delete')"
							>
								{{ $t('mkwlMbm3qr88f0XxV9Pje') }}
							</el-button>
						</div>
					</template>
				</inscroll-table>
			</el-card>
		</div>
		<training-plan-form ref="formRef" @submit="(param: any) => submitHandle(param)"></training-plan-form>
		<VModel :fileInfo="fileInfo" :isModelDialog="isModelDialog" @cancelModelClick="cancelModelClick" />
	</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { inscrollTable, planCourseTableConfig } from './config/table/index';
import { search, planSearch } from './config/search/index';
import getStaticTableData from '/@/utils/staticGetTableData';
import { getPlanList, postPlan, postChangePlan, postDeletePlan } from '/@/api/safety-play/planAPI';
import trainingPlanForm from './component/add-edit-training-plan.vue';
import { NORMSTARTTIMEfilter, uploadFiles } from '/@/utils/hook/index';

import { IPostPlanType } from '/@/types/ys/index';
import { ElMessageBox } from 'element-plus';
import commonRequest from '/@/api/common/common';
import eventBus from '/@/utils/eventBus';

import useCurrentInstance from '/@/utils/hook/useCurrentInstance';

import VModel from '../components/v-model.vue';

const { proxy } = useCurrentInstance();
const $t = proxy.$t;

const formRef = ref();
const { list, totalRows, handleSizeChange, handleCurrentChange, getData } = getStaticTableData(getPlanList, {}, planSearch.searchObj);
let isChange = false;

//附件删除后刷新列表
eventBus.on('getData', getData);
const add = () => {
	isChange = false;
	formRef.value.addNew({});
};
const query = () => {
	getData();
};
let currentChangeData: any;
const changeAction = (data: any) => {
	isChange = true;
	currentChangeData = data;
	formRef.value.change(data);
};
const detailAction = (data: any) => {
	formRef.value.detail(data);
};
const deleteAction = (data: any) => {
	ElMessageBox.confirm($t('itYcbTIj20MmWuPdBMgvd'), $t('nHdM3UcfLoZbHyqgSa3cx'), {
		confirmButtonText: $t('cpmrq09Mw3tfVmwWGsZzb'),
		cancelButtonText: $t('wFa44E4Iz025Ha2gYeDop'),
		type: 'warning',
		center: true,
	})
		.then(async () => {
			await postDeletePlan({
				n_Id: data.n_Id,
			});
			getData();
		})
		.catch(() => {});
};

//提交
const submitHandle = async (param: any) => {
	console.log('param', param);
	// let changeEmpStr = [];
	// //防止param.empStr,含有原来的oldEmpstr name数据
	// for (let i = 0; i < param.empStr.length; i++) {
	// 	if (formRef.value.oldEmpstr.indexOf(param.empStr[i]) == -1) {
	// 		changeEmpStr.push(param.empStr[i]);
	// 	}
	// }
	let tempFileID = 0; //计划附件id
	let tempImplementFileID = 0; //实施附件id
	//新增时获取雪花id为filedId赋值
	if (!isChange) {
		let res = await commonRequest.getGenerateId();
		tempFileID = res.data;

		let res1 = await commonRequest.getGenerateId();
		tempImplementFileID = res1.data;
	}

	let postData: IPostPlanType = {
		V_TrainName: param.v_TrainName,
		N_PlanCourse: isChange && currentChangeData.v_CourseName == param.n_PlanCourse ? currentChangeData.n_PlanCourse : param.n_PlanCourse,
		N_TeacherId: isChange && currentChangeData.v_EmpName == param.n_TeacherId ? currentChangeData.n_TeacherId : param.n_TeacherId,
		N_Durations: param.n_Durations,
		D_TrainTime: NORMSTARTTIMEfilter(param.d_TrainTime),
		V_TrainAddress: param.v_TrainAddress,
		V_AssessmentMethod: param.v_AssessmentMethod.join(),
		//判断empStr是否为修改前data的name信息,如果是改成oldEmpstrid
		// EmpStr: isChange && formRef.value.oldEmpstr == param.empStr.join() ? formRef.value.oldEmpstrID : changeEmpStr.join(),
		//现在修改时,不能修改培训人员,所以这里暂时穿原值
		EmpStr: isChange ? formRef.value.oldEmpstrID : param.empStr.join(),
		N_isValid: param.n_isValid,
		V_Remake: param.v_Remake,
		N_PlanContentId: isChange ? currentChangeData.n_PlanContentId : tempFileID,
		N_TrainContentId: isChange ? currentChangeData.n_TrainContentId : tempImplementFileID,
	};
	if (formRef.value.fileList.length > 0) {
		await uploadFiles(postData.N_PlanContentId, formRef.value.fileList);
	}

	if (isChange) {
		postData.n_Id = currentChangeData.n_Id;
		await postChangePlan(postData);
	} else {
		await postPlan(postData);
	}
	getData();
};

const isModelDialog = ref(false);
const cancelModelClick = () => {
	isModelDialog.value = false;
};
const fileInfo = ref('');
/*** 查看文件 */
const fileClick = (val: any) => {
	if (val.length == 0 || val == null) {
		return;
	}
	isModelDialog.value = true;
	let setObj: any = {};
	setObj.titleInfo = $t('s4KbtSrTfOzuWyRcHgTxx');
	setObj.rectifyListArr = val;
	fileInfo.value = JSON.stringify(setObj);
};
</script>

<!-- scoped属性使style有自己独立的作用域(属性选择器实现 title[data-v-xxx]) -->
<style lang="scss" scoped>
#bg {
	:deep(.el-card__body) {
		height: 100%;
	}
}
</style>
